package com.hulk.ratel.manage.commonservice;


import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.hulk.ratel.common.constant.enums.NotifyLogStatus;
import com.hulk.ratel.manage.bo.AsyncParamRespBO;
import com.hulk.ratel.persistence.entity.LogNotify;
import com.hulk.ratel.persistence.entity.LogTxnPayRecord;
import com.hulk.ratel.persistence.repository.ILogNotifyRepository;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @Author: hulk
 * @E-mail: 29572320@qq.com
 * @Date: Create in 2018/9/20 下午3:04
 * @Version: 1.0.0
 * @Modify by:
 * Class description
 */
@Slf4j
@AllArgsConstructor
@Component
public class PartnerNotifyService {


    private ILogNotifyRepository logNotifyRepository;

    private RetryNotifyService retryNotifyService;


    @Async(value = "eventBusAsync")
    public void dealNotify(AsyncParamRespBO bo,LogTxnPayRecord record,String workId) {

        LogNotify  notify = new LogNotify();
        Long notifyId = IdWorker.getId();
        notify.setId(notifyId);
        notify.setMaxNotifyConut(RetryNotifyService.MAX_NOTIFY_COUNT);
        notify.setNotifyCount(0);
        notify.setNotifyUrl(record.getCallBackUrl());
        notify.setPartnerNo(record.getPartnerNo());
        notify.setTxnCode(record.getTxnCode());
        notify.setStatus(NotifyLogStatus.INIT.getValue());
        notify.setOriTraceId(record.getTraceId());
        notify.setOriWorkId(record.getWorkId());
        logNotifyRepository.save(notify);
        retryNotifyService.doNotify( bo,  notify , record, notifyId, workId);
    }


}
